package org.basic.sms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.basic.common.core.utils.MapstructUtils;
import org.basic.common.core.utils.StringUtils;
import org.basic.common.mybatis.core.page.PageQuery;
import org.basic.common.mybatis.core.page.TableDataInfo;
import org.basic.sms.domain.SysSmsChannel;
import org.basic.sms.domain.bo.SysSmsChannelBo;
import org.basic.sms.domain.vo.SysSmsChannelVo;
import org.basic.sms.mapper.SysSmsChannelMapper;
import org.basic.sms.service.ISysSmsChannelService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 邮件渠道Service业务层处理
 *
 * @author Shao Hua
 * @date 2023-07-20
 */
@RequiredArgsConstructor
@Service
public class SysSmsChannelServiceImpl implements ISysSmsChannelService {

    private final SysSmsChannelMapper baseMapper;

    /**
     * 查询邮件渠道
     */
    @Override
    public SysSmsChannelVo queryById(Long id){
        SysSmsChannelVo sysSmsChannelVo = baseMapper.selectVoById(id);
//        sysSmsChannelVo.setSecretId(desensitizationStr(sysSmsChannelVo.getSecretId(), 4));
//        sysSmsChannelVo.setSecretKey(desensitizationStr(sysSmsChannelVo.getSecretKey(), 4));
        return sysSmsChannelVo;
    }

    /**
     * 查询邮件渠道列表
     */
    @Override
    public TableDataInfo<SysSmsChannelVo> queryPageList(SysSmsChannelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSmsChannel> lqw = buildQueryWrapper(bo);
        Page<SysSmsChannelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

//        List<SysSmsChannelVo> records = result.getRecords();
//        for(SysSmsChannelVo vo : records){
//            String secretId = vo.getSecretId();
//            String secretKey = vo.getSecretKey();
////            vo.setSecretId(desensitizationStr(secretId,6));
//            vo.setSecretId(desensitizationStr(secretId,6));
//            vo.setSecretKey(desensitizationStr(secretKey,6));
//        }
        return TableDataInfo.build(result);
    }

    private String desensitizationStr(String str,int visibleLength) {
        int start = (str.length() - visibleLength) / 2;
        int end = start + visibleLength;
        StringBuilder maskedData = new StringBuilder(str);
        for (int i = start; i < end; i++) {
            maskedData.setCharAt(i, '*');
        }
        return maskedData.toString();
    }

    /**
     * 查询邮件渠道列表
     */
    @Override
    public List<SysSmsChannelVo> queryList(SysSmsChannelBo bo) {
        LambdaQueryWrapper<SysSmsChannel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSmsChannel> buildQueryWrapper(SysSmsChannelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSmsChannel> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysSmsChannel::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), SysSmsChannel::getCode, bo.getCode());
        lqw.eq(bo.getType() != null, SysSmsChannel::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getSecretId()), SysSmsChannel::getSecretId, bo.getSecretId());
        lqw.eq(StringUtils.isNotBlank(bo.getSecretKey()), SysSmsChannel::getSecretKey, bo.getSecretKey());
        return lqw;
    }

    /**
     * 新增邮件渠道
     */
    @Override
    public Boolean insertByBo(SysSmsChannelBo bo) {
        SysSmsChannel add = MapstructUtils.convert(bo, SysSmsChannel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮件渠道
     */
    @Override
    public Boolean updateByBo(SysSmsChannelBo bo) {
        SysSmsChannel update = MapstructUtils.convert(bo, SysSmsChannel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSmsChannel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邮件渠道
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<SysSmsChannelVo> queryAll() {
        List<SysSmsChannel> sysSmsChannels = baseMapper.selectList(new LambdaQueryWrapper<>(SysSmsChannel.class)
            .eq(SysSmsChannel::getDelFlag, 0)
            .select(SysSmsChannel::getId,SysSmsChannel::getName));
        List<SysSmsChannelVo> result = sysSmsChannels.stream().map(data -> {
            SysSmsChannelVo vo = new SysSmsChannelVo();
            BeanUtil.copyProperties(data, vo);
            return vo;
        }).collect(Collectors.toList());
        return TableDataInfo.build(result);

    }
}
